//
//  ALPS_MainMoreViewController.m
//  Alps
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_MainMoreViewController.h"
#import "WSSNetTool.h"
#import "TYTabButtonPagerController.h"
#import "ALPS_MoreOtherViewController.h"
#import "ALPS_MoreRecommendViewController.h"
#import "ALPS_MainModel.h"

@interface ALPS_MainMoreViewController ()<TYPagerControllerDataSource>
/** 分页控制器 */
@property (nonatomic, strong) TYTabButtonPagerController *pagerController;
/** topView数组 */
@property (nonatomic, strong) NSMutableArray *topArray;
/** 标题数组 */
@property (nonatomic, strong) NSMutableArray *headerArray;
@end

@implementation ALPS_MainMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.navigationItemTitle;
    [self setLeftBackButton];
    [self addPagerController];
}

- (void)getControllerDataByID:(NSInteger)number
{
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=%ld&contentType=album&device=android&version=4.3.92", number] body:nil headers:nil response:WSSJSON success:^(id result) {
        self.topArray = [NSMutableArray array];
        NSDictionary *dic = [result objectForKey:@"tags"];
        NSArray *arr = [dic objectForKey:@"list"];
        for (NSDictionary *dict in arr) {
            [self.dataSource addObject:[dict objectForKey:@"tname"]];
        }
        //topView
        NSDictionary *topDic = [result objectForKey:@"categoryContents"];
        NSArray *topArr = [topDic objectForKey:@"list"];
        for (NSDictionary *dic in topArr) {
            ALPS_MainModel *model = [[ALPS_MainModel alloc] initWithDic:dic];
            if (model.list.count != 0) {
                [self.topArray addObject:model];
            }
        }
        [self.pagerController reloadData];
    } failure:^(NSError *error) {
        
    }];
    
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/tagsWithoutCover?categoryId=%ld&contentType=album&device=android&title=更多", number] body:nil headers:nil response:WSSJSON success:^(id result) {
        self.headerArray = [NSMutableArray array];
        NSArray *arr = [result objectForKey:@"list"];
        for (NSDictionary *dic in arr) {
            [self.headerArray addObject:[dic objectForKey:@"tname"]];
        }
    } failure:^(NSError *error) {
        
    }];

}

- (void)addPagerController
{
    TYTabButtonPagerController *pagerController = [[TYTabButtonPagerController alloc]init];
    pagerController.dataSource = self;
    pagerController.adjustStatusBarHeight = YES;
    pagerController.barStyle = TYPagerBarStyleProgressView;
    pagerController.cellSpacing = 8;
    pagerController.progressWidth = 36;
    pagerController.view.frame = self.view.bounds;
    [self addChildViewController:pagerController];
    [self.view addSubview:pagerController.view];
    _pagerController = pagerController;
    [self getControllerDataByID:self.categoryId];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    _pagerController.view.frame = self.view.bounds;
}

#pragma mark - TYPagerControllerDataSource

- (NSInteger)numberOfControllersInPagerController
{
    return self.dataSource.count;
}

- (NSString *)pagerController:(TYPagerController *)pagerController titleForIndex:(NSInteger)index
{
    return [self.dataSource objectAtIndex:index];
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index
{
    if (index == 0) {
        ALPS_MoreRecommendViewController *VC = [[ALPS_MoreRecommendViewController alloc]init];
        VC.categoryId = self.categoryId;
        VC.topArray = self.topArray;
        return VC;
    } else {
        ALPS_MoreOtherViewController *VC = [[ALPS_MoreOtherViewController alloc]init];
        VC.headerArray = self.headerArray;
        VC.categoryId = self.categoryId;
        VC.indexRow = index;
        VC.whichPage = 1;
        return VC;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
